package com.itqf.dao.impl;

import com.itqf.connecter.DbConnection;
import com.itqf.dao.TypeDao;
import com.itqf.entity.Type;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDaoImpl implements TypeDao {
    @Override
    public List<Type> selectAll() throws SQLException {
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        int rows = 0;
        conn= DbConnection.getConnection();
        sql = "select t_id as tid , t_name as tname , t_info as tinfo from type limit 9;";

        List<Type> tl = new ArrayList<Type>();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()) {
                int tid = rs.getInt(1);
                String tname = rs.getString(2);
                String tinfo  = rs.getString(3);
                Type type = new Type(tid,tname,tinfo);
                tl.add(type);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        DbConnection.close(rs, ps);
        return tl;
    }

    @Override
    public Type selectTypeByTid(int tid) {
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        int rows = 0;
        conn= DbConnection.getConnection();
        sql = "select t_id as tid , t_name as tname , t_info as tinfo from type where t_id = ?;";
        Type type = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,tid);
            rs = ps.executeQuery();
            while(rs.next()) {
                int tid2 = tid;
                String tname = rs.getString(2);
                String tinfo  = rs.getString(3);
                type = new Type(tid,tname,tinfo);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        DbConnection.close(rs, ps);
        return type;
    }
}
